package framework.common.baseui;

import android.Manifest;
import android.app.Activity;
import android.app.ActivityManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.PowerManager;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.app.AppCompatDelegate;
import android.support.v7.widget.SearchView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import net.zhuike.novel.app.R;
import net.zhuike.novel.app.ui.base.Constant;
import net.zhuike.novel.app.ui.utils.CoolEngine;
import net.zhuike.novel.app.ui.utils.SharedPreferencesUtil;
import net.zhuike.novel.app.ui.view.loadding.CustomDialog;

import java.util.List;

import butterknife.ButterKnife;

import static android.view.View.GONE;

public class UeBaseActivity extends AppCompatActivity {

    public Context context;
    private boolean mNowMode;
    private CustomDialog dialog;//进度条
    //是否是前台进程
    private boolean isActive = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub

        super.onCreate(savedInstanceState);
        context = this;
        setContentView(R.layout.baselayout);
        registerBoradcastReceiver();
        mNowMode = SharedPreferencesUtil.getInstance().getBoolean(Constant.ISNIGHT);
    }


    public void baseSetContentView(int layoutResId) {
        LinearLayout llContent = (LinearLayout) findViewById(R.id.content);
        LayoutParams params = new LayoutParams(
                FrameLayout.LayoutParams.MATCH_PARENT,
                FrameLayout.LayoutParams.MATCH_PARENT);
        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = inflater.inflate(layoutResId, null);
        llContent.addView(v, params);
        ButterKnife.bind(this);

    }

    public ImageView getGoBackBtn() {
        return (ImageView) findViewById(R.id.back);
    }

    public TextView getTitleView() {
        return (TextView) findViewById(R.id.title);
    }

    public ImageView getRTitleView() {
        return (ImageView) findViewById(R.id.right);
    }

    public void setTitleText(String title) {
        ((TextView) findViewById(R.id.title)).setText(title);
    }

    public TextView getMenuText() {
        return ((TextView) findViewById(R.id.menuTex));
    }

    public void setRightText() {
        ((ImageView) findViewById(R.id.right)).setVisibility(View.VISIBLE);
    }

    public EditText getSearch() {
        return (EditText) findViewById(R.id.action_search);
    }

    public void showGoBackBtn() {
        getGoBackBtn().setVisibility(View.VISIBLE);
        getGoBackBtn().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UeBaseActivity.this.finish();
            }
        });
    }

    public void hiddenGoBackBtn() {
        getGoBackBtn().setVisibility(View.GONE);
    }

    public void showHeader() {
        ((LinearLayout) findViewById(R.id.title_bar)).setVisibility(View.VISIBLE);
    }

    public void hiddenHeader() {
        ((LinearLayout) findViewById(R.id.title_bar)).setVisibility(View.GONE);
    }

    public void showFooter() {
        ((LinearLayout) findViewById(R.id.footerbar)).setVisibility(View.VISIBLE);
    }

    public void hiddenFooter() {
        ((LinearLayout) findViewById(R.id.footerbar)).setVisibility(View.GONE);
    }

    public BroadcastReceiver mFinishReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if ("com.uelive.main_finish".equals(intent.getAction())) {
                finish();
            }
        }
    };

    public void registerBoradcastReceiver() {
        IntentFilter myIntentFilter = new IntentFilter();
        myIntentFilter.addAction("com.uelive.main_finish");
        // 注册广播
        registerReceiver(mFinishReceiver, myIntentFilter);
    }

    public void unregisterBoradcastReceiver() {
        unregisterReceiver(mFinishReceiver);
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        unregisterBoradcastReceiver();
        ButterKnife.unbind(this);
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
    }

    @Override
    protected void onRestart() {
        // TODO Auto-generated method stub
        super.onRestart();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (SharedPreferencesUtil.getInstance().getBoolean(Constant.ISNIGHT, false) != mNowMode) {
            if (SharedPreferencesUtil.getInstance().getBoolean(Constant.ISNIGHT, false)) {
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
            } else {
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
            }
            //recreate();
        }
    }

    @Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
    }


    @Override
    protected void onStop() {
        super.onStop();

    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {

        super.onSaveInstanceState(outState);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        super.onActivityResult(requestCode, resultCode, data);
    }


    /**
     * 对各种控件进行设置、适配、填充数据
     */

    protected void gone(final View... views) {
        if (views != null && views.length > 0) {
            for (View view : views) {
                if (view != null) {
                    view.setVisibility(View.GONE);
                }
            }
        }
    }


    public CustomDialog getDialog() {
        if (dialog == null) {
            dialog = CustomDialog.instance(this);
            dialog.setCancelable(true);
        }
        return dialog;
    }

    public void hideDialog() {
        if (dialog != null)
            dialog.hide();
    }

    public void showDialog() {
        getDialog().show();
    }

    public void dismissDialog() {
        if (dialog != null) {
            dialog.dismiss();
            dialog = null;
        }
    }

    protected boolean isVisible(View view) {
        return view.getVisibility() == View.VISIBLE;
    }

    private void checkSelfPermission() {
        ActivityCompat.requestPermissions(((Activity) this),
                new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.WRITE_SETTINGS},
                1);
    }
}

